package com.example.myapplication01.chapter05;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;


import com.example.myapplication01.R;

import java.util.Calendar;

public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener,TimePickerDialog.OnTimeSetListener{
     TimePicker tp_time;
     TextView tv_time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_picker);
        findViewById(R.id.btn_ok).setOnClickListener( this);
        findViewById(R.id.btn_time).setOnClickListener(this);
        tp_time = findViewById(R.id.tp_time);
        tp_time.setIs24HourView(true);
        tv_time = findViewById(R.id.tv_time);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.btn_ok){
            String desc = String.format("您选择的时间是%d时%d分", tp_time.getHour(), tp_time.getMinute());
            tv_time.setText(desc);
        }
        if(v.getId()==R.id.btn_time){
            // 获取日历的一个实例，里面包含了当前的时分秒
            Calendar calendar = Calendar.getInstance();

            // 构建一个时间对话框，该对话框已经集成了时间选择器。
            TimePickerDialog dialog = new TimePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog,  this,
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    true); // true表示24小时制，false表示12小时制
            dialog.show();
        }
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String desc = String.format("弹出框选择的时间是%d时%d分", tp_time.getHour(), tp_time.getMinute());
        tv_time.setText(desc);
    }
}